
public class MainActivity extends Activity  implements OnPageChangeListener{
     
	   // 当前Fragment中的ViewPager
	   private ViewPager mViewPager = null;
	   // 展示当前页面标识的TextView
	   private TextView mTextView = null;
	   // 布局填充器对象
	   private LayoutInflater mLayoutInflater = null;
	   // 保存所有的ViewPager的View
	   private ArrayList<TextView> mViewList = new ArrayList<TextView>();
	   private ArrayList<String> dataList = new ArrayList<String>();
	
	   // 切换到左边的ImageView
	   private TextView mToLeftImageView = null;
	   // 切换到右侧的ImageView
	   private TextView mToRightImageView = null;
	   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

	 
	      mViewPager = (ViewPager) findViewById(R.id.bannerFragmentViewPager);
	      mTextView = (TextView) findViewById(R.id.countBannerFragmentTextView);
	 
//	      mToLeftImageView = (ImageView) mRootView.findViewById(R.id.toLeftBannerFragmentImageView);
//	      mToLeftImageView.setOnClickListener(BannerFragment.this);
//	 
//	      mToRightImageView = (ImageView) mRootView.findViewById(R.id.toRightBannerFragmentImageView);
//	      mToRightImageView.setOnClickListener(BannerFragment.this);
	 
	      initData();
	   
    }
    // 初始化数据
	   private void initData(){
	     for (int i = 0; i < 1; i++) {
	    	 dataList.add("当前是第"+(i+1)+"页");
		}
	     initViewPager();
	   }
	   
	   // 初始化ViewPager
	   private void initViewPager() {
		   int length =dataList.size();
		   if (dataList.size()>1) {
			   length=length+2;
		}
	      for (int i = 0; i < length; i++) {
	         TextView mViewPagerImageView = new TextView(this);
	         ViewGroup.LayoutParams viewPagerImageViewParams =
	               new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
	                     ViewGroup.LayoutParams.FILL_PARENT);
	         if(i==0||i == length-2){
	        	 mViewPagerImageView.setText(dataList.get(dataList.size() - 1));
		      }else if(i==1||i == length - 1){
		    	  mViewPagerImageView.setText(dataList.get(0));
		      }else{
		    	  mViewPagerImageView.setText(dataList.get(i - 1));
		      }
	         if (i%2==0) {
	        	 mViewPagerImageView.setBackgroundColor(Color.BLUE);
			}else{
				 mViewPagerImageView.setBackgroundColor(Color.GREEN);
			}
	         mViewPagerImageView.setLayoutParams(viewPagerImageViewParams);	         
	         mViewList.add(mViewPagerImageView);
	      }
	      if (mViewList != null && mViewList.size() > 0) {
	         mViewPager.setAdapter(new ViewpagerAdapter());
	      }
	      mViewPager.setOnPageChangeListener(this);
	 
	      if (dataList.size() == 0) {
	         mTextView.setText(String.format("0 / %s", dataList.size()));
	      } else {
	         mTextView.setText(String.format("1 / %s", dataList.size()));
	      }
	      mViewPager.setCurrentItem(1, false);
	   }
	   
	   /**
	    * PagerAdapter
	    * @author
	    *
	    */
	   class ViewpagerAdapter extends PagerAdapter {
	      @Override
	      public void destroyItem(View container, int position, Object object) {
	         TextView view = mViewList.get(position % mViewList.size());
	         ((ViewPager) container).removeView(view);
	         view.setText("");
	      }
	 
	      @Override
	      public Object instantiateItem(View container, int position) {
	         ((ViewPager) container).addView(mViewList.get(position));
			  if(position == 0){
		    	  mViewList.get(position).setText(dataList.get(dataList.size() - 1));
		      }else if(position == mViewList.size() - 1){
		    	  mViewList.get(position).setText(dataList.get(0));
		      }else{
		    	  mViewList.get(position).setText(dataList.get(position - 1));
		      }
	            return mViewList.get(position);
	      }
	 
	      @Override
	      public int getCount() {
	         return mViewList.size();
	      }
	 
	      @Override
	      public boolean isViewFromObject(View view, Object object) {
	         return view == object;
	      }
	   }
	 
	   @Override
	   public void onPageScrollStateChanged(int state) {
	 
	   }
	 
	   @Override
	   public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	 
	   }
	 
	   @Override
	   public void onPageSelected(int position) {
//	      if(position == 0){
//	    	  mViewList.get(position).setText(dataList.get(dataList.size() - 1));
//	      }else if(position == mViewList.size() - 1){
//	    	  mViewList.get(position).setText(dataList.get(0));
//	      }else{
//	    	  mViewList.get(position).setText(dataList.get(position - 1));
//	      }
	      int pageIndex = position;
	       if(position == 0){
	          pageIndex = dataList.size();
	       }else if(position == dataList.size() + 1){
	          pageIndex = 1;
	       }
	       if(position != pageIndex){
	          mViewPager.setCurrentItem(pageIndex, false);
	          return;
	       }
	       int count = mViewList.size() - 2;
	       if(mViewList != null && count > 0){
	          int index = (position);
	          String text = index + "/" + count;
	          mTextView.setText(text);
	       }else{
	         String text = 0 + " / " + 0;
	         mTextView.setText(text);
	       }
	   }
}